package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.Impl.EmployeeServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeServiceImpl employeeService;

    /**
     * 员工登入
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request,@RequestBody Employee employee){
       //1. 将页面提交的密码password进行 md5 加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2. 根据页面提交用户名 username 查询数据库
        LambdaQueryWrapper<Employee> employeeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        employeeLambdaQueryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(employeeLambdaQueryWrapper);
        // 3. 如果没有查询到则返回登入失败
        if (emp == null){
            return R.error("登入失败");
        }
        //4. 密码比对, 如果不一致则返回登入失败结果
        if(!emp.getPassword().equals(password)){
            return R.error("登入失败");
        }
        //5. 查询员工状态,如果为已禁用状态 则返回员工禁用结果
        if(emp.getStatus() == 0){
            return R.error("账号已禁用");
        }
        //6.登入成功 将员工对象 id 存入 session 并返回登入成功结果
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    /**
     * 员工退出
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //清除 Session 中保存的当前登入员工的 id
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 添加员工
     * @param request
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        log.info("添加员工的信息:{}"+employee.toString());
        //设置初始密码为 12345 然后为初始密码进行 md5的加密
        employee.setPassword(DigestUtils.md5DigestAsHex("12345".getBytes()));

        //设置自动填充配置

        //设置 创建时间 和 更新时间
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());
        //获取当前登入用户id
        //Long createUser = (Long) request.getSession().getAttribute("employee");
        //employee.setCreateUser(createUser);
        //employee.setUpdateUser(createUser);
        log.info("添加员工的信息:{}"+employee.toString());

        employeeService.save(employee);
        return R.success("添加成功");
    }

    /**
     * 员工信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //构造分页构造器
        Page paheinfo = new Page(page,pageSize);
        log.info("分页构造器"+paheinfo.toString());
        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        log.info("条件构造器"+queryWrapper.toString());
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        //执行查询寻
        employeeService.page(paheinfo,queryWrapper);
        return R.success(paheinfo);
    }

    /**
     * 修改员工状态
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> PutStatus(HttpServletRequest request,@RequestBody Employee employee){
        log.info("要修改员工的信息:{}",employee.toString());
        employee.setUpdateTime(LocalDateTime.now()) ;
        //MyMetaObjecthandler 使用自动填充
//        Long employeeId = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateUser(employeeId);

        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    /**
     * 使用 id 获取员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> queryEmployeeById(@PathVariable Long id){
        //使用 获取到的id 进行查询
        Employee byId = employeeService.getById(id);
        //将数据返回给前段
        return R.success(byId);
    }
}

